package controlLayer;
import modelLayer.*;
import java.util.ArrayList;

/**
 * Controls loans
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanController
{
    private LoanContainer lCon;
    private DVDController dCtrl;
    private PersonController pCtrl;

    /**
     * Constructor for objects of class LoanController
     */
    public LoanController()
    {
        lCon = lCon.getInstance();
        dCtrl = new DVDController();
        pCtrl = new PersonController();
    }

    /**
     * Method createLoan
     * 
     * creates a loan and adds it to the ArrayList loans in LoanController and Person
     *
     * @param id A parameter
     * @param borrowDate A parameter
     * @param period A parameter
     * @param phone A parameter
     * @param barcode A parameter
     * @param serialNumber A parameter
     * @return "personNotFound" if person was not found, "DVDNotfound" if DVD was not found, "DVDRentout" if DVD is rented out, "confirmed" for loan created
     */
    public String createLoan(String id, String borrowDate, String period, String phone, String barcode, String serialNumber)
    {
        if(pCtrl.findPerson(phone)==null)
        {
            return "personNotFound";
            //Person findes ikke
        }
        else
        {
            if(dCtrl.findCopy(barcode, serialNumber) == null)
            {
                return "DVDNotFound";
                //DVD findes ikke
            }
            else{
                if(dCtrl.findCopy(barcode, serialNumber).getState()==false)
                {
                    return "DVDRentOut";
                    //DVDen er udlånt
                }
                else{
                    Loan tempL = null;
                    Person tempP = pCtrl.findPerson(phone);
                    Copy tempC = dCtrl.findCopy(barcode, serialNumber);
                    if(findLoan(id) == null)
                    {
                        tempL = new Loan(tempP, tempC, id, borrowDate, period);
                        lCon.addLoan(tempL);
                        pCtrl.addLoan(tempL, phone);
                        tempC.setState(false);
                        return "confirmed";
                        //gj
                    }
                    else
                    {
                        return "loanExists";
                        //lån findes i forvejen
                    }
                }
            }
        }
    }

    /**
     * Method findLoan
     * 
     * searches for a loan from the given id
     *
     * @param id A parameter
     * @return The return value
     */
    public Loan findLoan(String id)
    {
        return lCon.findLoan(id);
    }

    /**
     * Method updateLoan
     * 
     * searches for the loan's id, and updates the loan's information
     *
     * @param oldId A parameter
     * @param borrowDate A parameter
     * @param period A parameter
     * @param newId A parameter
     * @return The return value
     */
    public boolean updateLoan(String oldId, String borrowDate, String period, String newId)
    {
        if(findLoan(oldId) == null)
        {
            return false;
        }
        else
        {
            return lCon.updateLoan(oldId, borrowDate, period, newId);
        }
    }

    /**
     * Method deleteLoan
     * 
     * sets the loan's state to false.
     *
     * @param id A parameter
     * @return The return value
     */
    public boolean deleteLoan(String id)
    {

        if(findLoan(id)==null)
        {
            return false;
        }
        else
        {
            Loan tempL = findLoan(id);
            tempL.setStateFalse();
            Copy tempC = tempL.getCopy();
            tempC.setState(true);
            return true;
        }
    }

    /**
     * Method getState
     *
     * @param id A parameter
     * @return boolean state
     */
    public boolean getState(String id)
    {
        return lCon.getState(id);
    }
    
    public ArrayList<Loan> getLoans()
    {
        return lCon.getLoans();
    }

}
